package cn.lbd;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ConfigurationCenter implements Watcher {
    private CountDownLatch countDownLatch = new CountDownLatch(1);
    private ZooKeeper zooKeeper = null;
    private String IP = "39.97.191.1:2181";
    private String url;
    private String username;
    private String password;

    @Override
    public void process(WatchedEvent event) {
        if (event.getType() == Event.EventType.None) { //通知状态被修改时，事件类型一定是None
            if (event.getState() == Event.KeeperState.SyncConnected) {
                System.out.println("连接成功");
                countDownLatch.countDown();
            }else if (event.getState() == Event.KeeperState.Disconnected) {
                System.out.println("断开连接");
            }else if (event.getState() == Event.KeeperState.Expired) {
                System.out.println("会话超时");
                try {
                    zooKeeper = new ZooKeeper(IP,5000,this);//超时就重新连接
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if (event.getState() == Event.KeeperState.AuthFailed) {
                System.out.println("授权失败");
            }
        }else if (event.getType() == Event.EventType.NodeDataChanged) { //监听到配置被修改的事件
            System.out.println("Watcher：Zookeeper中的配置信息有改动，进行自动更新");
            initValue();
        }
    }

    public ConfigurationCenter() {
        initValue();
    }

    public void initValue() {
        try {
            zooKeeper = new ZooKeeper(IP,5000,this);
            countDownLatch.await();
            //本地存储配置信息，通过监听器监听配置的变化，从而让Zookeeper自动更新配置信息
            this.url = new String(zooKeeper.getData("/info/url",true,null));
            this.username = new String(zooKeeper.getData("/info/username",true,null));
            this.password = new String(zooKeeper.getData("/info/password",true,null));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ConfigurationCenter configurationCenter = new ConfigurationCenter();
        while (true){
            System.out.println("url => "+configurationCenter.getUrl());
            System.out.println("username => "+configurationCenter.getUsername());
            System.out.println("password => "+configurationCenter.getPassword());
            System.out.println();
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
